package Learning;

import java.sql.*;

public class demo4 {
    public static void main(String[] args) {
        String url  = "jdbc:mysql://192.168.10.132:3306/learn_JDBC?useUnicode=true&characterEncoding=utf-8";

        Statement set = null;
        Connection con =  null;
        // 返回的结果集
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");

            con = DriverManager.getConnection(url,"root", "123456");
            set = con.createStatement();

            String sql = "select * from user";
            // int executeUpdata(insert/delete/updata)
            // ResultSet executeQuary(select)
            rs = set.executeQuery(sql); // 专门执行DQL(查询语句)
            // 处理查询结果集
//        boolean flag = rs.next();
//         System.out.println(flag); 结果为True 有数据为True
            while (rs.next()){
//            if (flag){
//                String a = rs.getString(1); // 下标从1开始
//                String b = rs.getString(2);
//                String c = rs.getString(3);
//                System.out.println(a + " | " + b + " | " + c);
//            }
                String a = rs.getString("user_id");  // 注意sql语句的as，根据sql语句语句来转换
                String b = rs.getString("user_name");
                String c = rs.getString("pwd");
                String d = rs.getString("user_rename");
                System.out.println(a + " | " + b + " | " + c + "|" + d);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (rs != null){
                    rs.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
            try {

                if (set != null){
                    set.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
            try {
                if (con != null){
                    con.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}
